package com.study.springcloud.database;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.study.springcloud.entites.Payment;
import com.study.springcloud.util.CommonUtils;

@Component(value = "dataBase")
public class DataBaseDao<T> implements DataBase<T>{

    private Map<String,String> dataBase;

    private static String SPLIT = "----";

   Logger log = Logger.getLogger("Logger");

    @PostConstruct
    public void init() throws Exception {
        log.info("----- init -----");
        dataBase = new HashMap<>();
        List<String> list = CommonUtils.readeFile("C:/数据库/database.txt");
        if(!CollectionUtils.isEmpty(list)){
            for(String temp : list){
                if(!StringUtils.isEmpty(temp)){
                    String[] split = temp.split(SPLIT);
                    dataBase.put(split[0],split[1]);
                }
            }
        }
    }

    @PreDestroy
    public void destory(){
        log.info("----- destory -----");
        List<String> list = new ArrayList<>();
        for(Map.Entry<String,String> entry : dataBase.entrySet()){
            list.add(entry.getKey()+SPLIT+entry.getValue());
        }
        try {
            CommonUtils.writeFile(list,"C:/数据库/database.txt");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public <T> void putData(T t){
        try{
            int key = t.hashCode();
            String value = JSON.toJSONString(t);
//        dataBase.put(key,value);

            Field[] declaredFields = t.getClass().getDeclaredFields();
            String id = null;
            for(Field filed : declaredFields){
                filed.setAccessible(true);
                Annotation[] annotations = filed.getAnnotations();
                for(Annotation annotation : annotations){
                    if(annotation.annotationType().getCanonicalName().equals("com.study.springcloud.annotation.Id")){
                        id = (String) filed.get(t);
                    }
                }
            }
            dataBase.put(id,value);
            List<String> list = new ArrayList<>();
            list.add(id+SPLIT+value);
            try {
                CommonUtils.writeFileAppend(list,"C:/数据库/database.txt");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public  <T> T getData(Object key,Class<T> t){
        String k = (String) key;
        String s = dataBase.get(k);
        T t1 = JSON.parseObject(s, t);
        return t1;
    }

    public static void main(String[] args) {
        Payment ment = new Payment();
        ment.setId("1");
        ment.setSerial("1");
        // DataBase.putData(ment);
    }

}
